package com.plugin.storage.storage.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.plugin.storage.base.AuthToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

@JsonIgnoreProperties(ignoreUnknown = true)
public class UploadFile {

    private static final Logger LOGGER = LoggerFactory.getLogger(UploadFile.class);

    /**
     * 文件url
     */
    private String fileUrl;

    /**
     * 文件访问url
     */
    private String accessUrl;

    /**
     * 文件key
     */
    private String key;

    /**
     * 本地文件
     */
    @JsonIgnore
    private File file;

    /**
     * 原始文件名
     */
    private String originFileName;

    public UploadFile() {
    }

    public UploadFile(String accessEndPoint, String key) {
        this.fileUrl = accessEndPoint + "/" + key;
        this.key = key;
    }

    public UploadFile(String accessEndPoint, String key, File file) {
        this(accessEndPoint, key);
        this.file = file;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public String getAccessUrl() {
        return accessUrl;
    }

    public UploadFile setAccessUrl(String accessUrl) {
        this.accessUrl = accessUrl;
        return this;
    }

    public String getKey() {
        return key;
    }

    public UploadFile setKey(String key) {
        this.key = key;
        return this;
    }

    public File getFile() {
        return file;
    }

    public UploadFile setFile(File file) {
        this.file = file;
        return this;
    }

    public String getOriginFileName() {
        return originFileName;
    }

    public UploadFile setOriginFileName(String originFileName) {
        this.originFileName = originFileName;
        return this;
    }

    @Override
    public String toString() {
        return "UploadFile{" +
                "fileUrl='" + fileUrl + '\'' +
                ", key='" + key + '\'' +
                '}';
    }

    public static class Auth extends AuthToken {

        private String endpoint;

        private String bucket;

        private String region;

        private String targetDir;

        private String key;

        public Auth() {
        }

        public Auth(AuthToken authToken) {
            setAccessKeyId(authToken.getAccessKeyId());
            setAccessKeySecret(authToken.getAccessKeySecret());
            setSecurityToken(authToken.getSecurityToken());
            setExpiration(authToken.getExpiration());
        }

        public String getEndpoint() {
            return endpoint;
        }

        public Auth setEndpoint(String endpoint) {
            this.endpoint = endpoint;
            return this;
        }

        public String getBucket() {
            return bucket;
        }

        public Auth setBucket(String bucket) {
            this.bucket = bucket;
            return this;
        }

        public String getRegion() {
            return region;
        }

        public Auth setRegion(String region) {
            this.region = region;
            return this;
        }

        public String getTargetDir() {
            return targetDir;
        }

        public Auth setTargetDir(String targetDir) {
            this.targetDir = targetDir;
            return this;
        }

        public String getKey() {
            return key;
        }

        public Auth setKey(String key) {
            this.key = key;
            return this;
        }

        @Override
        public String toString() {
            return "Auth{" +
                    "endpoint='" + endpoint + '\'' +
                    ", bucket='" + bucket + '\'' +
                    ", region='" + region + '\'' +
                    ", targetDir='" + targetDir + '\'' +
                    ", key='" + key + '\'' +
                    "} " + super.toString();
        }
    }

}
